﻿using NewCodeAPI.IServices;
using NewCodeAPI.Models;





namespace NewCodeAPI.Service
{
    public class OpenclassService : IOpenclassService
    {
        //开设课程
        public MsgInfo AddClass(ClassBegin classBeginCourse)
        {
            using (var context = new NdlsDBContext()) // 创建数据库上下文实例
            {
                var _base = context.BaseCourses.FirstOrDefault(b => b.Id == classBeginCourse.Id);
                if (_base != null)
                {
                    return new MsgInfo { Status = "Error", Msg = "课程已存在" };
                }
                else
                {
                    try
                    {
                        // 将用户对象添加到数据库
                        context.ClassBegins.Add(classBeginCourse);
                        context.SaveChanges(); // 保存更改到数据库
                        return new MsgInfo { Status = "Success", Msg = "添加成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "添加失败" };
                    }
                }
            }
        }

        //修改课程信息
        public MsgInfo ModifyClass(int del, string remark, ClassBegin classBeginCourse)
        {
            using (var context = new NdlsDBContext())
            {
                var existingclass = context.BaseCourses.FirstOrDefault(b => b.Del == del && b.Remark == remark);
                if (existingclass == null)
                {
                    return new MsgInfo { Status = "Error", Msg = "课程不存在" };
                }
                else
                {
                    try
                    {
                        // 更新课程信息
                        existingclass.Remark = classBeginCourse.Remark;
                        // 根据需要更新其他字段
                        context.SaveChanges(); // 保存更改到数据库

                        return new MsgInfo { Status = "Success", Msg = "更新成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "更新失败" };
                    }
                }
            }
        }

        //获取课程信息
        public List<MsgInfo> GetClassInfo(string remark)
        {
            using (var context = new NdlsDBContext())
            {
                var bases = context.ClassBegins.Where(b => b.Remark == remark).ToList();

                if (bases.Count == 0)
                {
                    return new List<MsgInfo> { new MsgInfo { Status = "Error", Msg = "没有该课程" } };
                }
                else
                {
                    List<MsgInfo> result = bases.Select(baseItem => new MsgInfo { Status = "Success", Msg = "有该课程" }).ToList();
                    return result;
                }
            }
        }

        //删除课程
        public MsgInfo DeleteClass(int del, string remark)
        {
            using (var context = new NdlsDBContext())
            {
                var _base = context.ClassBegins.FirstOrDefault(b => b.Del == del && b.Remark == remark);
                if (_base == null)
                {
                    return new MsgInfo { Status = "Error", Msg = "没有该课程，无法删除" };
                }
                else
                {
                    try
                    {
                        context.ClassBegins.Remove(_base);
                        context.SaveChanges();
                        return new MsgInfo { Status = "Success", Msg = "课程删除成功" };
                    }
                    catch
                    {
                        return new MsgInfo { Status = "Error", Msg = "课程删除失败" };
                    }

                }
            }
        }
    }
}
